﻿@inject IHttpService _httpService


@inject NavigationManager NavigationManager
@inject IJSRuntime JS
@inject IDataCacheService _dataCacheService

@implements IDisposable

@if (string.IsNullOrWhiteSpace(Model.DisplayName) == false)
{
    <CnGalWebSite.Shared.MasaComponent.Shared.Cards.TitleTip Title="@(Model.DisplayName)" Description="@Model.BriefIntroduction" Image="@Model.MainPicture" />
}
else
{
    <CnGalWebSite.Shared.MasaComponent.Shared.Cards.TitleTip />
}

@if (_dataCacheService.ThemeSetting.IsTransparent)
{

    @if (string.IsNullOrWhiteSpace(Model.SmallBackgroundPicture) == false)
    {
        <div id="web_bg" style="background-image: url(@Model.SmallBackgroundPicture)" class=" d-none  bg-d-lg-block bgimage-ison"></div>
    }
    @if (string.IsNullOrWhiteSpace(Model.BackgroundPicture) == false)
    {
        <div id="web_bg" style="background-image: url(@Model.BackgroundPicture)" class=" d-none d-sm-block bgimage-ison"></div>
    }

}



@if (isNotFound == true)
{
    <NotFoundCard>
        <h1 class="text-center title-large">找不到这个抽奖哦......</h1>
        <div class="text-center">如果链接没有错的话，可以试试联系管理员</div>
    </NotFoundCard>
}
else
{
    <div>
        @if (_dataCacheService.LotteryIndexPageCatche.Check(Id) || isReady)
        {
            @if (Model.IsHidden == true)
            {
                <CnGalWebSite.Shared.MasaComponent.Shared.Components.MasaAlert Type="AlertTypes.Warning" Class="mb-3">
                    当前抽奖处于隐藏状态，如果你看到了这个提示，请立即联系管理员
                </CnGalWebSite.Shared.MasaComponent.Shared.Components.MasaAlert>

            }

            @if (_dataCacheService.IsApp)
            {
                <div class="row">
                    <div class="col-12 col-lg-9">

                        <CnGalWebSite.Shared.Component.Lotteries.MainPage.MainInforCard Model="Model" StateChanged="HiddenStateChanged" />

                        <CnGalWebSite.Shared.Component.Lotteries.MainPage.BasicInforCard Model="Model" />
                        <CnGalWebSite.Shared.Component.Lotteries.MainPage.OptionInforCard ViewModel="Model" />


                        <CnGalWebSite.Shared.Component.Lotteries.MainPage.MainPageCard Model="Model" />

                        <CnGalWebSite.Shared.Component.Lotteries.MainPage.AwardListCard Model="Model.Awards" />

                        @if (Model.Awards.Any(s => s.Users.Any()))
                        {
                            <CnGalWebSite.Shared.Component.Lotteries.MainPage.WinningUserListCard Model="Model.Awards" />
                        }

                        <CnGalWebSite.Shared.MasaComponent.PC.Comments.CommentTip CanComment="Model.CanComment" CanDeleted="IsAdmin" ObjectId="@Model.Id.ToString()" Type="CommentType.CommentLottery" OnChanged="OnChanged"/>



                    </div>
                </div>
            }
            else
            {
                <div class="row">
                    <div class="col-12 col-lg-9">

                        <CnGalWebSite.Shared.Component.Lotteries.MainPage.MainInforCard Model="Model" StateChanged="HiddenStateChanged" />
                        <CnGalWebSite.Shared.Component.Lotteries.MainPage.MainPageCard Model="Model" />


                        <CnGalWebSite.Shared.Component.Lotteries.MainPage.AwardListCard Model="Model.Awards" />

                        <CnGalWebSite.Shared.MasaComponent.PC.Comments.CommentTip CanComment="Model.CanComment" CanDeleted="IsAdmin" ObjectId="@Model.Id.ToString()" Type="CommentType.CommentLottery" OnChanged="OnChanged" />
                    </div>
                    <div class="col-12 col-lg-3">

                        <CnGalWebSite.Shared.Component.Lotteries.MainPage.BasicInforCard Model="Model" />

                        <CnGalWebSite.Shared.Component.Lotteries.MainPage.OptionInforCard ViewModel="Model" />
                        <CnGalWebSite.Shared.Component.Lotteries.MainPage.WinningUserListCard Model="Model.Awards" />
                    </div>
                </div>
            }

        }
        else
        {
            <CnGalWebSite.Components.Progressings.ProgressingCard Page />
        }
    </div>
}

<CnGalWebSite.Shared.MasaComponent.Shared.Components.TrackEvent DataType="TrackEventDataType.Lottery" EventType="TrackEventType.View" ObjectId="@Model.Id.ToString()"
                                                                ObjectName="@Model.Name" />
@if (isFirstLoad == false)
{
    <CnGalWebSite.Shared.MasaComponent.Shared.Cards.NavLoadingCard IsLoading="isLoading" />
}


@code {
    [Parameter]
    public string Id { get; set; }

    bool isReady = false;
    bool isNotFound = false;
    public StringNumber TabIndex { get; set; } = 1;

    public LotteryViewModel Model { get; set; } = new LotteryViewModel();

    public PagedResultDto<CommentViewModel> ListComments = new PagedResultDto<CommentViewModel> { Data = new List<CommentViewModel>(), CurrentPage = 1, TotalCount = 0 };
    public bool IsAdmin { get; set; } = false;
    string LastLookName = "";
    bool isFrist = true;
    bool isLoading = false;
    bool isFirstLoad = true;

    [CascadingParameter]
    private Task<AuthenticationState> authenticationStateTask { get; set; }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        await base.OnAfterRenderAsync(firstRender);
        if (firstRender)
        {
            NavigationManager.LocationChanged -= LocationChanged;
            NavigationManager.LocationChanged += LocationChanged;
            isFirstLoad = false;
        }
    }

    protected override async Task OnInitializedAsync()
    {
        await Refresh();
    }
    protected async Task Refresh()
    {
        try
        {
            //获取周边信息
            Model = await _dataCacheService.LotteryIndexPageCatche.GetCache(Id);
            //判断投票名称是否为空 为空则跳转到主页
            if (string.IsNullOrWhiteSpace(Model.Name))
            {
                NavigationManager.NavigateTo("/votes/home");
            }

            isReady = true;
            //   StateHasChanged();
            var user = await authenticationStateTask;
            if (user.User.Identity.IsAuthenticated)
            {
                IsAdmin = user.User.IsInRole("Editor");
            }
            else
            {
                _dataCacheService.LotteryIndexPageCatche.Clean(Id);
            }
        }
        catch (Exception exc)
        {
            isReady = true;
            isNotFound = true;
            StateHasChanged();
        }

    }

    public async Task OnChanged()
    {
        if(Model.ConditionType== LotteryConditionType.CommentLottery)
        {
            isReady = false;
            StateHasChanged();
            _dataCacheService.LotteryIndexPageCatche.Clean(Id);
        await Refresh();
 
        }
    }

    public void HiddenStateChanged()
    {
        Model.IsHidden = !Model.IsHidden;
        StateHasChanged();
    }

    public async void LocationChanged(object sender, LocationChangedEventArgs args)
    {
        if (Id != Model.Id.ToString())
        {
            if (_dataCacheService.VideoIndexPageCatche.Check(Id) == false)
            {
                isLoading = true;
                StateHasChanged();
                await Refresh();
                isLoading = false;
                StateHasChanged();
            }
            else
            {
                await Refresh();
                StateHasChanged();
            }

        }
    }

    #region 释放实例

    public void Dispose()
    {

        NavigationManager.LocationChanged -= LocationChanged;

        GC.SuppressFinalize(this);
    }
    #endregion
}
